package us.icitap.commandcenter.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import us.icitap.commandcenter.business.ejb.persistence.Call;
import us.icitap.commandcenter.ui.shared.GeneralVars;

public class Utils {

	public static Date[] ShiftLimits(){
		
		Calendar c = Calendar.getInstance();
		
		Date startShift, endShift;
		
		if(c.get(Calendar.HOUR_OF_DAY) < 6){
			
			c.set(Calendar.HOUR_OF_DAY, 6);
			endShift = c.getTime();
			
			c.roll(Calendar.DATE, false);
			c.set(Calendar.HOUR_OF_DAY, 22);
			c.set(Calendar.MINUTE, 0);
			c.set(Calendar.SECOND, 0);
			
			startShift = c.getTime();
		
		}
		else if(c.get(Calendar.HOUR_OF_DAY) < 14){
			
			c.set(Calendar.HOUR_OF_DAY, 6);
			c.set(Calendar.MINUTE, 0);
			c.set(Calendar.SECOND, 0);
			startShift = c.getTime();
			
			c.set(Calendar.HOUR_OF_DAY, 14);
			endShift = c.getTime();
			
		}
		else if(c.get(Calendar.HOUR_OF_DAY) < 22){
			c.set(Calendar.HOUR_OF_DAY, 14);
			c.set(Calendar.MINUTE, 0);
			c.set(Calendar.SECOND, 0);
			startShift = c.getTime();
			
			
			c.set(Calendar.HOUR_OF_DAY, 22);
			c.set(Calendar.MINUTE, 0);
			c.set(Calendar.SECOND, 0);
			endShift = c.getTime();
		}
		else {
			c.set(Calendar.HOUR_OF_DAY, 22);
			c.set(Calendar.MINUTE, 0);
			c.set(Calendar.SECOND, 0);
			startShift = c.getTime();
			
			c.add(Calendar.DATE, 1);
			c.set(Calendar.HOUR_OF_DAY, 6);
			c.set(Calendar.MINUTE, 0);
			c.set(Calendar.SECOND, 0);
			endShift = c.getTime();
		}
		System.out.println(startShift + "\n" + endShift);
		return new Date[]{startShift,endShift};
	
	}
	
	public static Date[] AllDayShiftLimits(Date date){
		
		Date startShift, endShift;
		
		Calendar c = Calendar.getInstance();
		c.setTime(date);
//		c.set(Calendar.DAY_OF_MONTH, 30);
//		c.set(Calendar.MONTH, 4);
		
		c.add(Calendar.DATE, -1);
		c.set(Calendar.HOUR_OF_DAY, 6);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		
		startShift = c.getTime();
		
		c.add(Calendar.DATE, 1);
		endShift = c.getTime();
		
		System.out.println(startShift + "\n" + endShift);
		return new Date[]{startShift,endShift};
	
	}

	public static String getCallInfo(Call call){
			System.out.println(call.getRelatedCallList());
			String callInfo =  " <h2>Informacion mbi telefonaten</h2>" +
					"<table> " + 
			"<tr>" +
			"<td >Telefonuesi:</td>" +
			"<td class='boldLabels'>"+call.getCaller()+"</td>" +
			"</tr>" +
			"<tr>" +
			"<td>Koha e tel.:</td>" +
			"<td class='boldLabels'>"+call.getRecordDate()+"</td>" +
			"</tr>" +
			"<tr>" +
			"<td>Problemi:</td>" +
			"<td class='boldLabels'>"+((call.getCallReason()!=null)?call.getCallReason().getTag():"") +"</td>" +
			"</tr>" +
			"<tr>" +
			"<td>Pershkrimi:</td>" +
			"<td class='boldLabels'>"+call.getDescription()+"</td>" +
			"</tr>" +
			"<tr>" +
			"<td>Adresa:</td>" +
			"<td class='boldLabels'>"+call.getAddress()+"</td>" +
			"</tr>" +
			"</table>";
		if(call.getRelatedCallList() != null)
			for (Call relCall : call.getRelatedCallList()) {
				callInfo += " <h2>Telefonate tjeter e lidhur </h2>" +
						"<table> " + 
						"<tr>" +
						"<td >Telefonuesi:</td>" +
						"<td class='boldLabels'>"+relCall.getCaller()+"</td>" +
						"</tr>" +
						"<tr>" +
						"<td>Koha e tel.:</td>" +
						"<td class='boldLabels'>"+relCall.getRecordDate()+"</td>" +
						"</tr>" +
						"<tr>" +
						"<td>Problemi:</td>" +
						"<td class='boldLabels'>"+((relCall.getCallReason()!=null)?relCall.getCallReason().getTag():"") +"</td>" +
						"</tr>" +
						"<tr>" +
						"<td>Pershkrimi:</td>" +
						"<td class='boldLabels'>"+relCall.getDescription()+"</td>" +
						"</tr>" +
						"<tr>" +
						"<td>Adresa:</td>" +
						"<td class='boldLabels'>"+relCall.getAddress()+"</td>" +
						"</tr>" +
						"</table>";
			}
			
			return callInfo;
		
		
	}
	
	
	public static String formatedDate(Date date){
		SimpleDateFormat sdf = new SimpleDateFormat(GeneralVars.SHORT_DATE_FORMAT);
		if(date != null) return sdf.format(date);
		return null;
	}
	
	public static String formatedDateTime(Date date){
		SimpleDateFormat sdf = new SimpleDateFormat(GeneralVars.DATE_TIME_FORMAT);
		if(date != null) return sdf.format(date);
		return null;
	}
	
	public static String formatedDateTime1(Date date){
		SimpleDateFormat sdf = new SimpleDateFormat(GeneralVars.DATE_TIME_FORMAT_1);
		if(date != null) return sdf.format(date);
		return null;
	}
	
	public static String formatedTime(Date date){
		SimpleDateFormat sdf = new SimpleDateFormat(GeneralVars.TIME_FORMAT);
		if(date != null) return sdf.format(date);
		return null;
	}
	
}
